package com.xiu.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import com.xiu.util.DateUtil;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiu.pojo.ExamUser;
import com.xiu.service.ExamUserService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value="/exam")
public class ExamController {
	@Autowired
	ExamUserService examUserService;
	
	ExamUser examUser = new ExamUser();
	
	public void init(Model model,ExamUser examUser) throws Exception{
	}
	
	
	//显示所有ExamUser的信息
		@RequestMapping(value="/examList",method = RequestMethod.GET)
		public String findExamUser(Model model,ExamUser examUser){
			List<ExamUser> examList = examUserService.findAllExamUser(0);
			model.addAttribute("examlist", examList);
			model.addAttribute("examUser", examUser);
			return "exam/teacherMain";
		}
		
		//删除
		@RequestMapping(value="/delete/{examStuId}",method = RequestMethod.GET)
		public String deleteExamUser(Model model,@PathVariable int examStuId,RedirectAttributes re){
			int count = examUserService.deleteExamStu(examStuId);
			if(count == 1){
				List<ExamUser> examList = examUserService.findAllExamUser(0);
				model.addAttribute("examlist", examList);
				re.addFlashAttribute("msg", "删除成功");
			}else{
				List<ExamUser> examList = examUserService.findAllExamUser(0);
				model.addAttribute("examlist", examList);
				re.addFlashAttribute("msg", "删除失败");
				return "exam/teacherMain";
			}
			return "redirect:/exam/examList";
		}
		
		//增加2：增加成功后回到examList界面
		@RequestMapping(value="/addExam",method = RequestMethod.POST)
		public void add(@ModelAttribute ExamUser examUser,Model model,RedirectAttributes re,HttpServletResponse response) throws IOException{
			examUser.setExamBirthday(DateUtil.stringToDate(examUser.getExamBirthdayString()));
			int count = examUserService.insertUser(examUser);
			JSONObject json = new JSONObject();
			try {
				if(count == 1){
					List<ExamUser> examList = examUserService.findAllExamUser(0);
					model.addAttribute("examlist", examList);
					re.addFlashAttribute("addMsg", "增加成功");
					json.put("result", "success");
				}else{
					json.put("result", "error");
					//re.addFlashAttribute("addMsg", "增加失败");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			PrintWriter pw = response.getWriter();
			pw.write(json.toString());
			pw.close();
		}
		
		//修改1：跳转静态模态框
		@RequestMapping(value="/edit/{id}",method = RequestMethod.GET)
		public void edit(@PathVariable int id,Model model,HttpServletResponse response)throws IOException, JSONException{
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			examUser = examUserService.selectExamStu(id);
			JSONObject json = new JSONObject();
			JSONArray array= JSONArray.fromObject(examUser);
			json.put("exam", array);
			try {
				response.getWriter().write(json.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		
		
		//修改2：静态模态框修改
		@RequestMapping(value="/edit",method = RequestMethod.POST)
		public void edit(@ModelAttribute ExamUser examUser,Model model,HttpServletResponse response)throws IOException{
			examUser.setExamBirthday(DateUtil.stringToDate(examUser.getExamBirthdayString()));
			int count = examUserService.updateExamStu(examUser);
			JSONObject json = new JSONObject();
			try {
				if(count == 1){
					List<ExamUser> examList = examUserService.findAllExamUser(0);
					model.addAttribute("examlist", examList);
					json.put("result", "success");
				}else{
					json.put("result", "error");
					//re.addFlashAttribute("addMsg", "增加失败");
					}
				}catch (JSONException e) {
					e.printStackTrace();
					}
				PrintWriter pw = response.getWriter();
				pw.write(json.toString());
				pw.close();
			}
		
		//测试
		/*@RequestMapping(value="/user",method=RequestMethod.GET)
		public String goUser(){
			return "exam/user";
		}*/
}
